package com.yang.doctorrecruitment.dao;

import com.yang.doctorrecruitment.dto.CompanyBase;
import com.yang.doctorrecruitment.utils.DBManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class CompanyBaseDAO {
    private static Connection conn;
    public static boolean insertCompany(CompanyBase CompanyBase, String UserID,String photo){
        conn= DBManager.getconnection();
        ResultSet res=null;
        PreparedStatement statement=null;

        String sql=" insert into company values(?,?,?,?,?,?,?,?,?,?,?,?)";
        try{
            statement=conn.prepareStatement(sql);
            statement.setString(1,UserID);
            statement.setString(2,CompanyBase.getCompanyName());
            statement.setString(3,CompanyBase.getCompanyCorporation());
            statement.setString(4,CompanyBase.getCompanyAddress());
            statement.setString(5,CompanyBase.getCompanyIntroduce());
            statement.setString(6,CompanyBase.getCompanyNature());
            statement.setInt(7,CompanyBase.getCompanyScale());
            statement.setInt(8,CompanyBase.getCompanyType());
            statement.setInt(9,0);
            statement.setString(10,CompanyBase.getCompanyTelphone());
            statement.setString(11,CompanyBase.getCompanyEmail());
            statement.setString(12,photo);
            int count=statement.executeUpdate();
            if(count>=1){
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBManager.closeAll(conn,statement,res);
        }
        return false;
    }
    public static CompanyBase selectCompany(String UserID){
        conn= DBManager.getconnection();
        ResultSet res=null;
        PreparedStatement statement=null;
        CompanyBase companyBase=new CompanyBase();
        String sql=" select * from company where UserID=?";
        try{
            statement=conn.prepareStatement(sql);
            statement.setString(1,UserID);
            res=statement.executeQuery();
           while(res.next()){
               companyBase.setCompanyName(res.getString("CompanyName"));
               companyBase.setCompanyCorporation(res.getString("CompanyCorporation"));
               companyBase.setCompanyAddress(res.getString("CompanyAddress"));
               companyBase.setCompanyEmail(res.getString("CompanyEmail"));
               companyBase.setCompanyIntroduce(res.getString("CompanyIntroduce"));
               companyBase.setCompanyNature(res.getString("CompanyNature"));
               companyBase.setCompanyScale(res.getInt("CompanyScale"));
               companyBase.setCompanyTelphone(res.getString("CompanyTelphone"));
               companyBase.setCompanyType(res.getInt("CompanyType"));
               companyBase.setFilePath(res.getString("CompanyLicense"));
               companyBase.setCompanyIdentified(res.getInt("CompanyIdentified"));
               return companyBase;


           }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBManager.closeAll(conn,statement,res);
        }
        return null;
    }
}
